package Solution101_200.problem110.BalancedBinaryTree;

import common.TreeNode;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/07/12 10:01
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int leftDeep=getDepth(root.left,1);
        int rightDeep=getDepth(root.right,1);
        if(Math.abs(leftDeep-rightDeep)>1){
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);

    }

    private int getDepth(TreeNode root,int deep){
        if(root==null){
            return deep;
        }
        return Math.max(getDepth(root.left,deep+1),getDepth(root.right,deep+1));

    }
}
